<?php
/**
 * Template tags to use in your WordPress theme templates or plugins
 * Notice: the function prefix has changed from <code>lg_</code> to <code>lzg_</code> since Lazyest Gallery 2.0.0
 * 
 * @package Lazyest Gallery
 * @subpackage Template Tags
 * @since 2.0.0 (r440)
 */ 
  
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;  


// template functions ---------------------------------------------------------
/**
 * lzg_get_template_part()
 * Wrap for Lazyest_Frontend::get_template_part()
 * @see LazyestFrontend::get_template_part()
 * 
 * @since 2.0.0 (r440)
 * @param string $slug
 * @param string $name
 * @return void
 */
function lzg_get_template_part( $slug, $name = null ) {
	lazyest_frontend()->get_template_part( $slug, $name );
}

// gallery output functions ---------------------------------------------------
/**
 * lzg_gallery_title()
 * Wrap for Lazyest_Gallery::gallery_title()
 * @see Lazyest_Gallery::gallery_title()
 * 
 * @since 2.0.0 (r440)
 * @return void
 */
function lzg_gallery_title() {
	echo lazyest_gallery()->gallery_title();
}

/**
 * lzg_selector()
 * Returns selector for gallery element
 * 
 * @since 2.0.0 (r440)
 * @param bool $increase wether to increase the id number
 * @param bool $echo wether to echo the selector or return
 * @return string lazyest-gallery-$selector
 */
function lzg_selector( $increase= true, $echo = true ) {
	static $instance = 0;
	if ( $increase )
		$instance++;
	$selector = "lazyest-gallery-{$instance}";
	
	if ( $echo )
		echo $selector;
	else	
		return $selector;
}

/**
 * lzg_gallery_class()
 * Wrap for Lazyest_Frontend::gallery_class()
 * @see Lazyest_Frontend::gallery_class()
 * 
 * @since 2.0.0 (r440)
 * @param string $type
 * @return void
 */
function lzg_gallery_class( $type = 'archive' ) {
	echo lazyest_frontend()->gallery_class( $type );
}

/**
 * lzg_gallery_style()
 * Wrap for Lazyest_Frontend::gallery_style
 * Filter to override including style element:
 * <code>'use_default_gallery_style'</code> (bool)
 * 
 * @since 2.0.0 (r440)
 * @uses apply_filters()
 * @return string style element
 */
function lzg_gallery_style() {
	// bail if themes do not use default style
	if ( apply_filters( 'use_default_gallery_style', false ) )
		return;
		
	$selector = lzg_selector( true, false );
	echo lazyest_frontend()->gallery_style( $selector );
}

/**
 * lzg_itemtag()
 * Wrap for Lazyest_Frontend::itemtag()
 * @see Lazyest_Frontend::itemtag()
 * 
 * @since 2.0.0 (r440)
 * @return void
 */
function lzg_itemtag() {
	echo lazyest_frontend()->itemtag();
}

/**
 * lzg_itemtag()
 * Wrap for Lazyest_Frontend::icontag()
 * @see Lazyest_Frontend::icontag()
 * 
 * @since 2.0.0 (r440)
 * @return void
 */
function lzg_icontag() {
	echo lazyest_frontend()->icontag();
}

/**
 * lzg_folders_break()
 * Wrap for Lazyest_Frontend::folders_break()
 * @see  Lazyest_Frontend::folders_break()
 * 
 * @since 2.0.0 (r440) 
 * @param int $i counter
 * @return void
 */
function lzg_folders_break( $i ) {
	echo lazyest_frontend()->folders_break( $i );
}

/**
 * lzg_folder_thumbnail()
 * Wrap for Lazyest_Frontend::folder_thumbnail_html()
 * @see Lazyest_Frontend::folder_thumbnail_html()
 * 
 * @since 2.0.0 (r440)
 * @param integer $post_id
 * @return void
 */
function lzg_folder_thumbnail( $post_id = 0 ) {
	echo lazyest_frontend()->folder_thumbnail_html( $post_id );
} 

/**
 * lzg_folder_thumbnail()
 * Wrap for Lazyest_Frontend::folder_icon_caption()
 * @see Lazyest_Frontend::folder_icon_caption()
 * 
 * @since 2.0.0 (r440)
 * @param integer $post_id
 * @return void
 */
function lzg_folder_icon_caption( $post_id = 0 ) {
	lazyest_frontend()->folder_icon_caption( $post_id );
}

/**
 * lzg_folder_thumbnail()
 * Wrap for Lazyest_Frontend::folder_attachments_count()
 * @see Lazyest_Frontend::folder_attachments_count()
 * 
 * @since 2.0.0 (r440)
 * @param integer $post_id
 * @return void
 */
function lzg_folder_attachments_count( $post_id = 0 ) {
	lazyest_frontend()->folder_attachments_count( $post_id );
}

/**
 * lzg_folder_thumbnail()
 * Wrap for Lazyest_Frontend::breadcrumb()
 * @see Lazyest_Frontend::breadcrumb()
 * 
 * @since 2.0.0 (r440)
 * @param integer $post_id
 * @return void
 */
function lzg_breadcrumb( $post_id = 0 ) {
	lazyest_frontend()->breadcrumb( $post_id );
}

/**
 * lzg_folder_thumbnail()
 * Wrap for Lazyest_Frontend::thumbnails()
 * @see Lazyest_Frontend::thumbnails()
 * 
 * @since 2.0.0 (r440)
 * @param integer $post_id
 * @return void
 */
function lzg_thumbnails( $post_id = 0 ) {
	lazyest_frontend()->thumbnails( $post_id );
}

/**
 * lzg_folder_thumbnail()
 * Wrap for Lazyest_Frontend::subfolders()
 * @see Lazyest_Frontend::subfolders()
 * 
 * @since 2.0.0 (r440)
 * @param integer $post_id
 * @return void
 */
function lzg_subfolders( $post_id = 0 ) {
	lazyest_frontend()->subfolders( $post_id );
}

// template tags --------------------------------------------------------------

/**
 * lzg_list_folders()
 * Output or return an unordered list of galleryfolder posts
 * 
 * @since 2.0.0 (r440)
 * @uses wp_list_pages() to produce the listing
 * @param string $title
 * @param string $echo if not 'echo', markup is returned
 * @return void | string unordered list
 */
function lzg_list_folders( $title = '', $echo = 'echo' ) {
	list( $sort_column, $sort_order ) = explode( '-', lazyest_gallery()->sort_by() );
	$output = '<ul>' . wp_list_pages( array( 
		'post_type' => lazyest_gallery()->post_type, 
		'title_li' => $title, 
		'sort_column' => $sort_column, 
		'sort_order' => $sort_order, 
		'post_status' => 'publish', 
		'echo' => 0 ) 
		) . '</ul>';
	
	if ( 'echo' == $echo )
		echo $output;
	else
		return $output;		
}

/**
 * lzg_random_image()
 * Output a gallery with one or more random images from a folder with or without subfolders
 * 
 * @since 2.0.0 (r440)
 * @uses absint()
 * @uses esc_html()
 * @uses get_post
 * @uses WP_Post
 * @uses wp_get_attachment_image_src()
 * @uses wp_get_attachment_link
 * @param array $attr
 * <code>array(
		'number'     => 1,
		'columns'    => lazyest_gallery option  'thumbs_columns',
		'title'      => '',
		'subfolders' => 0,
		'size'       => 'thumbnail'
	)</code>
 * @return void
 */
function lzg_random_image( $attr = array() ) {
	extract( shortcode_atts( array(
		'number'     => 1,
		'columns'    => lazyest_gallery()->get_option( 'thumbs_columns' ),
		'title'      => '',
		'subfolders' => 0,
		'size'       => 'thumbnail'
	), $attr ) );
	$post_number = max( 1, absint( $number ) );
	
	$columns = absint( $columns );
	if ( $columns != lazyest_gallery()->get_option( 'thumbs_columns' ) )
		lazyest_gallery()->change_option( 'thumbs_columns', $columns );
	if ( isset( $folder ) && ! isset( $id ) )
		$id = lazyest_folderbase()->get_folder_by_string( $folder );
	if ( $id ) {
		$post_ids = "= $id";
		if ( $subfolders )
			if ( $random = lazyest_gallery()->get_option( 'random_subfolder' ) ){
				$children = lazyest_folderbase()->get_folder_children( $id );
				if ( ! empty( $children ) ) {
					$children[] = $id;
					$childlist = implode( ',', $children );
					$post_ids = "IN ($childlist)";
				} 				
			}
			$results = $wpdb->get_results( "
				SELECT ID FROM $wpdb->posts 
				WHERE post_parent $post_ids 
				AND post_type = 'attachment' 
				AND post_status IN ('inherit', 'publish') 
				ORDER BY RAND() 
				LIMIT $number;", 
				ARRAY_A 
			);
			if ( ! empty( $results ) ) :
				?>
				<?php if ( ! empty( $title ) ) : ?>
				<h3><?php echo esc_html( $title ); ?></h3>
				<?php endif; ?>
				<?php lzg_gallery_style() ?>
				<div id="<?php lzg_selector( false, true ) ?>" class="gallery lazyest-gallery gallery-size-<?php echo $size; ?>">
					<?php 
						$i = 0;
						foreach( $results as $result ) : 
							$attachment = get_post( $result['ID'] );
							$wp_sr      = wp_get_attachment_image_src( $attachment->ID, $size );
							$link       = lazyest_frontend()->add_attr_to_link( wp_get_attachment_link( $attachment->ID, $size ), $attachment->ID ); 
					?>
					<<?php lzg_itemtag(); ?> class="gallery-item">							
						<<?php lzg_icontag(); ?> class="gallery-icon">
							<?php echo $link ?>
						</<?php lzg_icontag(); ?>>
					</<?php lzg_itemtag(); ?>>
					<?php lzg_folders_break( ++$i ); ?>
					<?php	endforeach; ?>
					<br style="clear:both;"/>
				</div>
			<?php endif;
	}	
}

// include support for deprecated tags
include( lazyest_gallery()->plugin_dir . '/frontend/deprecated-tags.php' );